package br.com.timecardforpeon.adapter;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import br.com.timecardforpeon.R;
import br.com.timecardforpeon.dialog.RemoveDialog;
import br.com.timecardforpeon.domain.CheckType;
import br.com.timecardforpeon.domain.Entry;
import br.com.timecardforpeon.event.EventPublisher;
import br.com.timecardforpeon.event.impl.UpdateEntryEvent;
import br.com.timecardforpeon.util.Util;

public class EntryAdapter extends ArrayAdapter<Entry> {

	private final Activity activity;
	private final List<Entry> entries;

	private int[] colors;

	public EntryAdapter(Activity activity, List<Entry> entries) {

		super(activity, R.layout.entries_list, entries);

		this.activity = activity;
		this.entries = entries;

		int black = this.activity.getResources().getColor(R.color.white);
		int darkGray = this.activity.getResources().getColor(R.color.lightGray);
		this.colors = new int[] { black, darkGray };
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = null;

		if (convertView == null) {
			LayoutInflater inflater = this.activity.getLayoutInflater();
			view = inflater.inflate(R.layout.entries_list, null);
			createViewHolder(view);
		} else {
			view = convertView;
		}

		final ViewHolder holder = (ViewHolder) view.getTag();

		holder.checkIn.setText(this.entries.get(position).getCheckInTime());
		holder.checkIn.setTag(this.entries.get(position));

		holder.checkOut.setText(this.entries.get(position).getCheckOutTime());
		holder.checkOut.setTag(this.entries.get(position));

		holder.totalHours.setText(this.entries.get(position).getTotalHoursAsString());
		holder.totalHours.setTag(this.entries.get(position));

		int colorPos = position % this.colors.length;
		view.setBackgroundColor(this.colors[colorPos]);

		return view;
	}

	private ViewHolder createViewHolder(View view) {

		final ViewHolder holder = new ViewHolder();
		holder.checkIn = Util.<TextView> getViewById(view, R.id.checkInItem);
		holder.checkOut = Util.<TextView> getViewById(view, R.id.checkOutItem);
		holder.totalHours = Util.<TextView> getViewById(view, R.id.totalHoursItem);

		holder.checkIn.setOnClickListener(new CheckInClickListener());
		holder.checkOut.setOnClickListener(new CheckOutClickListener());

		holder.checkIn.setOnLongClickListener(new EntryLongClickListener(this.activity));
		holder.checkOut.setOnLongClickListener(new EntryLongClickListener(this.activity));
		holder.totalHours.setOnLongClickListener(new EntryLongClickListener(this.activity));

		view.setTag(holder);

		return holder;
	}

	private final class ViewHolder {

		protected TextView checkIn;
		protected TextView checkOut;
		protected TextView totalHours;
	}

	private final class CheckInClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {

			Entry entry = (Entry) view.getTag();
			Date checkIn = entry.getCheckIn();

			ChangedTimeListener listener = new ChangedTimeListener(entry, CheckType.IN);
			new TimePickerDialog(view.getContext(), listener, checkIn.getHours(), checkIn.getMinutes(), true).show();
		}

	}

	private final class CheckOutClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {

			Entry entry = (Entry) view.getTag();
			Date checkOut = entry.getCheckOut();

			if (checkOut == null) {
				return;
			}

			ChangedTimeListener listener = new ChangedTimeListener(entry, CheckType.OUT);
			new TimePickerDialog(view.getContext(), listener, checkOut.getHours(), checkOut.getMinutes(), true).show();
		}
	}

	private final class EntryLongClickListener implements OnLongClickListener {

		private Activity activity;

		public EntryLongClickListener(Activity activity) {
			super();
			this.activity = activity;
		}

		@Override
		public boolean onLongClick(View view) {

			Entry entry = (Entry) view.getTag();
			new RemoveDialog(this.activity, entry).show();
			return true;
		}
	}

	private final class ChangedTimeListener implements OnTimeSetListener {

		private Entry entry;
		private CheckType type;

		public ChangedTimeListener(Entry entry, CheckType type) {
			super();
			this.entry = entry;
			this.type = type;
		}

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

			if (this.type == CheckType.IN) {
				Date checkIn = this.entry.getCheckIn();
				checkIn.setHours(hourOfDay);
				checkIn.setMinutes(minute);
			} else {
				Date checkOut = this.entry.getCheckOut();
				checkOut.setHours(hourOfDay);
				checkOut.setMinutes(minute);
			}
			new EventPublisher().publish(new UpdateEntryEvent(this.entry));
		}
	}
}